/**
 * @author Rudy Kaplan Depena (The University of Texas at Austin)
 * 
 * Special Thanks to Oren Lewis Freiberg (UCLA) as he introduced me to
 * Java Tree Builder (JTB), a syntax tree building program. To learn more
 * about JTB, please visit http://compilers.cs.ucla.edu/jtb/
 * 
 * Author's Notes: This particular class is a highly modified version
 * of a generated JTB class called DepthFirstVisitor, found in the
 * visitor package of this compiler.
 */

package com.utaustin.mirankerlab.ultrawrap.compiler.main;

import com.utaustin.mirankerlab.ultrawrap.compiler.components.Component;
import com.utaustin.mirankerlab.ultrawrap.compiler.components.PrefixMap;
import com.utaustin.mirankerlab.ultrawrap.compiler.components.Triple;
import com.utaustin.mirankerlab.ultrawrap.compiler.components.FilterConstraint;
import com.utaustin.mirankerlab.ultrawrap.compiler.components.RelationalExpr;
import com.utaustin.mirankerlab.ultrawrap.compiler.parser.syntaxtree.*;
import com.utaustin.mirankerlab.ultrawrap.compiler.parser.syntaxtree.Object;
import com.utaustin.mirankerlab.ultrawrap.compiler.parser.syntaxtree.String;
import com.utaustin.mirankerlab.ultrawrap.compiler.parser.visitor.GJNoArguVisitor;
import com.utaustin.mirankerlab.ultrawrap.compiler.utilities.Utilities;
import com.utaustin.mirankerlab.ultrawrap.compiler.utilities.BookKeeper;
import com.utaustin.mirankerlab.ultrawrap.compiler.utilities.Stack;
import com.utaustin.mirankerlab.ultrawrap.compiler.exceptions.IncompleteRelationalExpressionException;
import com.utaustin.mirankerlab.ultrawrap.compiler.expressionTree.sparql.EnumOperator;
import com.utaustin.mirankerlab.ultrawrap.compiler.expressionTree.sparql.ExpressionTree;
import com.utaustin.mirankerlab.ultrawrap.compiler.expressionTree.sparql.BGP;
import com.utaustin.mirankerlab.ultrawrap.compiler.expressionTree.sparql.LeftJoin;

import java.util.Enumeration;
import java.util.List;
import java.util.ArrayList;

/** A visitor that visits different nodes in the parse tree to assist in SPARQL to SQL compilation */
public class SparqlToSql implements GJNoArguVisitor<Component> {

	//static variables
	public static PrefixMap prefixMap = new PrefixMap();
	public static Stack bracketStack = new Stack();
	public static BookKeeper bookKeeper = new BookKeeper();//keeps track of first encountered components
	//private static ExpressionTree exprTree = new ExpressionTree();
	
	//instance variables
	private boolean debug;//for debug statements
	private int tripleId;//keep track triples by numbering them (which are table views in RDBMS)
	private ExpressionTree exprTree;
	private List<Component> selectVars;
	private boolean distinct;
	private FilterConstraint filteredConstraint;
	private java.lang.String sqlQuery;
	

	/**
	 * Default Constructor
	 */
	public SparqlToSql(boolean _debug){
		//set debug flag to true if we want System.err statements, else false
		debug = _debug;
		tripleId = 0;
		exprTree = new ExpressionTree();
		selectVars = new ArrayList<Component>();
		distinct = false;
		filteredConstraint = null;
		sqlQuery = "";
	}
	
	/**
	 * Get the resulting SQL Query
	 * 
	 * @return String Sql query
	 */
	public java.lang.String getSqlQuery(){
		bookKeeper.emptyBookKeeper(); //added by juan: need to clear the BookKeeper
		return sqlQuery;
	}
	
	/**
	 * Get the list of tokens in the SELECT construct
	 * 
	 * @return ArrayList<String> List of Select tokens
	 */
	public ArrayList<java.lang.String> getSelectTokens(){
		ArrayList<java.lang.String> tokens = new ArrayList<java.lang.String>();
		for(int i=0; i<selectVars.size(); i++){
			tokens.add(selectVars.get(i).getSanitizedImg());
		}
		return tokens;
	}
	
	private void updateTriple(Component component, java.lang.String spo){
		int lastTripleIdx = Triple.triples.size() - 1;//get latest triple
		if(spo.equals("s") && Triple.triples.size() > 0){
			//if subject has not been set
			if(Triple.triples.get(lastTripleIdx).getSubjectOrSubjectKey() == null){
				//get the triple and set its subject
				Triple.triples.get(lastTripleIdx).setSubjectOrSubjectKey(component);
			}	
		}
		else if(spo.equals("p") && Triple.triples.size() > 0){
			//if subject has not been set
			if(Triple.triples.get(lastTripleIdx).getPredicate() == null){
				//get the triple and set its subject
				Triple.triples.get(lastTripleIdx).setPredicate(component);
			}
		}
		else if(spo.equals("o") && Triple.triples.size() > 0){
			//if subject has not been set
			if(Triple.triples.get(lastTripleIdx).getObjectOrObjectKey() == null){
				//get the triple and set its subject
				Triple.triples.get(lastTripleIdx).setObjectOrObjectKey(component);
			}
		}
	}

	//TODO for now all methods return null. This is useless and needs to be changed eventually.
	//
	// Auto class visitors--probably don't need to be overridden.
	//
	public Component visit(NodeList n) {
		for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); )
			e.nextElement().accept(this);
		return null;
	}

	public Component visit(NodeListOptional n) {
		if ( n.present() )
			for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); )
				e.nextElement().accept(this);
		return null;
	}

	public Component visit(NodeOptional n) {
		if ( n.present() )
			n.node.accept(this);
		return null;
	}

	public Component visit(NodeSequence n) {
		for ( Enumeration<Node> e = n.elements(); e.hasMoreElements(); )
			e.nextElement().accept(this);
		return null;
	}

	/**
	 * Visits a NodeToken in the parse tree 
	 * which could consist of a variable, 
	 * term, iri reference, putative literal, or literal.
	 * <p>
	 * Will return a Component which will contain the token
	 * in question.
	 * 
	 * @param NodeToken
	 * @return Component
	 */
	public Component visit(NodeToken n) {
		//capture token image
		java.lang.String token = n.tokenImage;
		
		//wrap in component
		Component nodeToken = new Component(token, tripleId);
		
		//if <, >, <=, >=, then operator has been found 
		//and should be placed in filtered regular expression
		if(filteredConstraint != null)
		{
			java.lang.String operator = nodeToken.getRawImg();
			if(operator.equals(">") || operator.equals(">=") || operator.equals("<")
			   || operator.equals("<="))
			{
				filteredConstraint.setOperator(nodeToken);	
			}
		}
		
		//add node token component to the book keeper's 
		//list (list of first encountered) components
		bookKeeper.addComponent(nodeToken);
		
		if(debug && nodeToken != null)
		{
			System.err.println("NodeToken = " + nodeToken.getRawImg());
		}
		
		//return the component
		return nodeToken;
	}

	//
	// User-generated visitor methods below
	//

	/**
	 * Visits an Query node in the parse tree.
	 * Query is the root node in the tree.
	 * This node is responsible for a tree that 
	 * represents a sparql query.
	 * 
	 * <p>
	 *  Will return a Component which will contain the token
	 * in question.
	 * 
	 * <p>
	 * While visiting Query, we visit its children, namely
	 * Prologue and NodeChoice, which are nodes that contain information
	 * in regards to the prologue of the query and the actual query itself.
	 * 
	 * <p>
	 * NodeChoice could be any number of query flavors(e.g. - SelectQuery,
	 * ConstructQuery, DescribeQuery, and AskQuery)
	 * 
	 * <p>
	 * For instance, <code> PREFIX bsbm: <http://www4.wiwiss.fu-berlin.de/bizer/bsbm/v01/vocabulary/> </code>
	 * is considered the prologue, while <code> SELECT ?product ?label WHERE{?product bsbm:productPropNum3 ?label} </code>
	 * is the SelectQuery.
	 *
	 * <PRE>
	 * prologue -> Prologue()
	 * nodeChoice -> ( SelectQuery() | ConstructQuery() | DescribeQuery() | AskQuery() )
	 * </PRE>
	 * 
	 * @param Query
	 * @return Component
	 * 
	 */
	public Component visit(Query n) {
		//TODO still need to figure out what to implement here
		n.prologue.accept(this);
		n.nodeChoice.accept(this);
		if(debug){
			System.err.println("FIRST ENCOUNTERED COMPONENTS");
			System.err.println(bookKeeper.toString());
			//exprTree.inOrderTraversal(exprTree.getRoot());
		}
		return null;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a Prologue node and
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents the prologue
	 * in a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The children of Prologue are the following:
	 * <ul>
	 * 	<li> NodeOptional is a node which contains BaseDecl (these are 
	 * 		 base declarations of the form <code>BASE <http://example.org/book/></code>)
	 *  <li> NodeListOptional is a node which contains PrefixDecl (these are
	 *  	 prefix declarations of the form <code>PREFIX book: <http://example.org/book/></code>)
	 * </ul>
	 * These declarations are considered part of the Prologue of the query.
	 * 
	 * <PRE>
	 * nodeOptional -> ( BaseDecl() )?
	 * nodeListOptional -> ( PrefixDecl() )*
	 * </PRE>
	 * 
	 * @param Prologue
	 * @param Component
	 */
	public Component visit(Prologue n) {
		//TODO still need to figure out what to implement here
		Component prologue = null;
		
		prologue = n.nodeOptional.accept(this);
		
		if(prologue != null)
			return prologue;
		
		prologue = n.nodeListOptional.accept(this);
		
		if(debug){
			if(prologue != null)
				System.err.println("Prologue = " + prologue);
			else{
				System.err.println("Prologue == null");
			}
		}
		
		if(prologue != null)
			return prologue;
		
		return null;
	}

	/**
	 * <p>
	 * Visit method will take a BaseDecl node and
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents the base
	 * declaration in the sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The BaseDecl node contains children which are the following:
	 * <ul>
	 * 	<li> NodeToken which is a node that contains 
	 * 		 the token <code>BASE</code>
	 *  <li> NodeToken1 which contains an iri reference 
	 *  	 such as <code><http://example.org/book/></code>
	 * </ul>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;BASE&gt;
	 * nodeToken1 -> &lt;IRI_REF&gt;
	 * </PRE>
	 * 
	 * @param BaseDecl
	 * @return Component
	 */
	public Component visit(BaseDecl n) {
		//TODo still need to figure out what to implement here
		n.nodeToken.accept(this);
		n.nodeToken1.accept(this);
		return null;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a PrefixDecl (this is a prefix declaration) 
	 * node and traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents the prefix
	 * declarations within a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The following example shows a prefix declaration:
	 * <code>
	 * PREFIX book: <http://example.org/book/>
	 * </code>
	 * 
	 * <p>
	 * The BaseDecl node contains children which are the following:
	 * <ul>
	 * 	<li> NodeToken that contains a <code> PREFIX </code>
	 * 	<li> NodeToken1 that contains a prefix name <code> book: </code>
	 *  <li> NodeToken2 that contains an iri reference <code> <http://example.org/book/> </code>
	 * </ul>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;PREFIX&gt;
	 * nodeToken1 -> &lt;PNAME_NS&gt;
	 * nodeToken2 -> &lt;IRI_REF&gt;
	 * </PRE>
	 * 
	 * @param PrefixDecl
	 * @return Component
	 */
	public Component visit(PrefixDecl n) {
		n.nodeToken.accept(this);
		n.nodeToken1.accept(this);
		n.nodeToken2.accept(this);
		
		java.lang.String tag = n.nodeToken1.tokenImage;
		java.lang.String iri = n.nodeToken2.tokenImage;
		
		if(debug){
			System.err.println("tag = " + tag);
			System.err.println("iri = " + iri);
		}
		
		prefixMap.addPrefix(tag, iri);
		
		return null;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a SelectQuery node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents the select query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * SelectQuery has many children within its subtree that form the
	 * query. Since SelectQuery is the root node in this subtree, here
	 * are the children:
	 * <ul>
	 * 	<li> NodeToken contains <code>SELECT</code>
	 * 	<li> NodeOptional contains either <code>DISTINCT</code> or <code>REDUCED</code>
	 * 	<li> NodeChoice contains a set of variables or an <code>*</code>
	 *       to select all
	 *  <li> NodeListOptional contains a DatasetClause
	 *  <li> WhereClause contains a set of triples
	 *  <li> SolutionModifier contains either order by or limit clauses
	 * </ul>
	 * 
	 * <p>
	 * The following is an example of a simple select query in sparql
	 * for which this tree represents:
	 * <code>
	 * SELECT DISTINCT ?s
	 * WHERE {?s ?p ?o}
	 * </code>
	 * Notice how the WhereClause would produce a tree that represents the
	 * clause containing a triple <code>WHERE {?s ?p ?o }</code>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;SELECT&gt;
	 * nodeOptional -> ( &lt;DISTINCT&gt; | &lt;REDUCED&gt; )?
	 * nodeChoice -> ( ( Var() )+ | "*" )
	 * nodeListOptional -> ( DatasetClause() )*
	 * whereClause -> WhereClause()
	 * solutionModifier -> SolutionModifier()
	 * </PRE>
	 * 
	 * @param SelectQuery
	 * @return Component
	 */
	public Component visit(SelectQuery n) {
		n.nodeToken.accept(this);
		n.nodeOptional.accept(this);
		
		if(n.nodeOptional.node != null && n.nodeOptional.node.toString().equals("DISTINCT")){
			distinct = true;
		}
		
		//n.nodeChoice.accept(this);
		if(n.nodeChoice.choice instanceof NodeList){
			for(Enumeration<Node> e = ((NodeList)n.nodeChoice.choice).elements(); e.hasMoreElements(); ){

				Component selectQuery = e.nextElement().accept(this);

				if(debug && selectQuery != null){
					System.err.println("SelectQuery = " + selectQuery);
				}

			}

		}
		n.nodeListOptional.accept(this);
		n.whereClause.accept(this);
		n.solutionModifier.accept(this);//TODO may have to place order by code here
		
		/*
		System.out.println("-- ULTRAWRAP COMPILER GENERATED SQL");
		System.out.println("");
		System.out.println(Utilities.sqlSelectVariables(selectVars, bookKeeper, distinct));
		System.out.println(exprTree.generateSql());
		*/
		sqlQuery += Utilities.sqlSelectVariables(selectVars, bookKeeper, distinct)+" ";
		sqlQuery += exprTree.generateSql();
		
		
		return null;
	}

	/**
	 * <p>
	 * Visits a ConstructQuery node in the parse tree.
	 * This node is responsible for a tree that represents a
	 * construct query.
	 * 
	 * <p>
	 * Will return a Component which will contain the token
	 * in question.
	 * 
	 * <p>
	 * This ConstructQuery node contains children:
	 * 
	 * <p>
	 * 	<ul>
	 * 		<li> NodeToken which contains a <code>CONSTRUCT</code> token
	 * 		<li> ConstructTemplate which contains a set of triples
	 * 		<li> NodeListOptional which contains a DatasetClause
	 * 		<li> WhereClause which contains a WhereClause with triples
	 * 		<li> SolutionModifier which contains order and limit clauses
	 * 	</ul>
	 * 
	 * <p>
	 * For example the following is a construct query,
	 * 	<code>
	 * CONSTRUCT   { <http://example.org/person#Alice> vcard:FN ?name }
	 * WHERE       { ?x foaf:name ?name }
	 * 	</code>
	 * 
	 * <p>
	 * In the example above the ConstructTemplate contains
	 * <code><http://example.org/person#Alice> vcard:FN ?name</code>
	 * While the WhereClause contains <code>?x foaf:name ?name</code>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;CONSTRUCT&gt;
	 * constructTemplate -> ConstructTemplate()
	 * nodeListOptional -> ( DatasetClause() )*
	 * whereClause -> WhereClause()
	 * solutionModifier -> SolutionModifier()
	 * </PRE>
	 * 
	 * @param ConstructQuery
	 * @return Component
	 */
	public Component visit(ConstructQuery n) {
		n.nodeToken.accept(this);
		Component constructQuery = n.constructTemplate.accept(this);
		n.nodeListOptional.accept(this);
		n.whereClause.accept(this);
		n.solutionModifier.accept(this);
		
		if(debug && constructQuery != null)
			System.err.println("ConstructQuery = " + constructQuery);
		
		return constructQuery;
	}

	/**
	 * <p>
	 * Visits a DescribeQuery node in the parse tree.
	 * This node is responsible for a tree that represents a
	 * describe query.
	 * 
	 * <p>
	 * Will return a Component which will contain the token
	 * in question.
	 * 
	 * <p>
	 * This DescribeQuery node contains children:
	 * 
	 * <p>
	 * 	<ul>
	 * 		<li> NodeToken which contains a <code>DESCRIBE</code> token
	 * 		<li> NodeChoice which contains a variable or iri reference
	 * 		<li> NodeListOptional which contains a set of data
	 * 		<li> NodeOptional which contains a WhereClause
	 * 		<li> SolutionModifier which contains order and limit clauses
	 * 	</ul>
	 * 
	 * <p>
	 * For example the following is a describe query,
	 * 	<code>
	 * 	DESCRIBE ?x
	 * 	WHERE    { ?x foaf:mbox <mailto:alice@org> }
	 * 	</code>
	 * 
	 * <p>
	 * The triple <code> ?x foaf:mbox <mailto:alice@org> </code>
	 * is contained by NodeOptional.
	 * 
	 * <PRE>
	 * nodeToken -> &lt;DESCRIBE&gt;
	 * nodeChoice -> ( ( VarOrIRIref() )+ | "*" )
	 * nodeListOptional -> ( DatasetClause() )*
	 * nodeOptional -> ( WhereClause() )?
	 * solutionModifier -> SolutionModifier()
	 * </PRE>
	 * 
	 * @param DescribeQuery
	 * @return Component
	 */
	public Component visit(DescribeQuery n) {
		//TODO don't know how to implement, looks a lot like SelectQuery()
		n.nodeToken.accept(this);
		
		//n.nodeChoice.accept(this);
		if(n.nodeChoice.choice instanceof NodeList){
			for(Enumeration<Node> e = ((NodeList)n.nodeChoice.choice).elements(); e.hasMoreElements(); ){
				Component describeQuery = e.nextElement().accept(this);
				//TODO most likely add subject or object tags here (perhaps like SelectQuery)
			}
		}
		
		n.nodeListOptional.accept(this);
		n.nodeOptional.accept(this);
		n.solutionModifier.accept(this);
		return null;//TODO Do I need to return something here?
	}

	/**
	 * <p>
	 * Visit method will take a AskQuery node and
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents an ask query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The children of the AskQuery subtree, where AskQuery
	 * is the root, are the following:
	 * <ul>
	 * 	<li> NodeToken contains the ASK token
	 * 	<li> NodeListOptional contains a DatasetClause
	 * 	<li> WhereClause contains a set of triples
	 * </ul>
	 * 
	 * <p>
	 * For example:
	 * <code>
	 * ASK  { ?x foaf:name  "Alice" ;
          foaf:mbox  <mailto:alice@work.example> }
	 * </code>
	 * Where the two triples in the example are contained in the DatasetClause.
	 * 
	 * <PRE>
	 * nodeToken -> &lt;ASK&gt;
	 * nodeListOptional -> ( DatasetClause() )*
	 * whereClause -> WhereClause()
	 * </PRE>
	 * 
	 * @param AskQuery
	 * @return Component
	 */
	public Component visit(AskQuery n) {
		n.nodeToken.accept(this);
		Component askQuery = n.nodeListOptional.accept(this);
		n.whereClause.accept(this);
		
		if(debug && askQuery != null)
			System.err.println("AskQuery = " + askQuery);
		
		return askQuery;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a DatasetClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a DatasetClause in a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Since DatasetClause is a root node in a subtree representing
	 * a clause of data then its children are the following:
	 * <ul>
	 * 	<li> NodeToken contains the token <code>FROM</code>
	 * 	<li> NodeChoice contains either a DefaultGraphClause 
	 * 		 or a NamedGraphClause.
	 * </ul>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;FROM&gt;
	 * nodeChoice -> ( DefaultGraphClause() | NamedGraphClause() )
	 * </PRE>
	 * 
	 * @param DatasetClause
	 * @return Component
	 */
	public Component visit(DatasetClause n) {
		n.nodeToken.accept(this);
		Component datasetClause = n.nodeChoice.accept(this);
		
		if(debug && datasetClause != null)
			System.err.println("DatasetClause = " + datasetClause);
		
		return datasetClause;
	}

	/**
	 * <p>
	 * Visit method will take a DefaultGraphClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a DefaultGraphClause in a dataset clause in a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Since DefaultGraphClause is a root node in a subtree representing
	 * a default graph clause inside the dataset clause 
	 * then its children are the following:
	 * <ul>
	 * 	<li> NodeOptional contains the <code>WHERE</code> token
	 * 	<li> GroupGraphPattern contains the body of the where clause
	 * </ul>
	 * 
	 * <PRE>
	 * sourceSelector -> SourceSelector()
	 * </PRE>
	 * 
	 * @param DefaultGraphClause
	 * @return Component
	 */
	public Component visit(DefaultGraphClause n) {
		Component defaultClause = n.sourceSelector.accept(this);
		
		if(debug && defaultClause != null)
			System.err.println("DefaultClause = " + defaultClause);
		
		return defaultClause;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a NamedGraphClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a NamedGraphClause in a named graph clause in a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * An example of this named clause is rooted in the default graph
	 * <code>FROM NAMED <http://example.org/alice><code>
	 * 
	 * <p>
	 * The NamedGraphClause has children in this subtree:
	 * <ul>
	 * 	<li>NodeToken contains <code>NAMED</code>
	 * 	<li>SourceSelector contains a source selector which is essentially
	 * 		an iri. In the example above, a source selector would be
	 * 		<code> <http://example.org/alice> </code>
	 * </ul>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;NAMED&gt;
	 * sourceSelector -> SourceSelector()
	 * </PRE>
	 * 
	 * @param NamedGraphClause
	 * @return Component
	 */
	public Component visit(NamedGraphClause n) {
		n.nodeToken.accept(this);
		Component namedGraphClause = n.sourceSelector.accept(this);
		
		if(debug && namedGraphClause != null)
			System.err.println("NamedGraphClause = " + namedGraphClause );
		
		return namedGraphClause;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a SourceSelector node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a source selector (i.e. - an iri).
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The child node of the parent SourceSelector node is an IRIRef which 
	 * could look like <code> <http://example.org/alice> </code> in the 
	 * named graph clause example <code>FROM NAMED <http://example.org/alice><code>   
	 * 
	 * <PRE>
	 * iRIref -> IRIref()
	 * </PRE>
	 * 
	 * @param SourceSelector
	 * @return Component
	 */
	public Component visit(SourceSelector n) {
		Component sourceSelector = n.iRIref.accept(this);
		
		if(debug && sourceSelector != null)
			System.err.println("SourceSelector = " + sourceSelector);
		
		return sourceSelector;
	}

	/**
	 * <p>
	 * Visit method will take a WhereClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a WhereClause in a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Since WhereClause is a root node in a subtree representing
	 * a where clause then its children are the following:
	 * <ul>
	 * 	<li> NodeOptional contains the <code>WHERE</code> token
	 * 	<li> GroupGraphPattern contains the body of the where clause
	 * </ul>
	 * 
	 * <p>
	 * For instance in the following code block:
	 * <code>
	 * SELECT ?name
	 * WHERE { ?x foaf:name ?name }
	 * </code>
	 * contains the NodeOptional (i.e. - <code>WHERE</code>)
	 * and the GroupGraphPattern (i.e. - <code>{ ?x foaf:name ?name }</code>)
	 *
	 * <PRE>
	 * nodeOptional -> ( &lt;WHERE&gt; )?
	 * groupGraphPattern -> GroupGraphPattern()
	 * </PRE>
	 * 
	 * @param WhereClause
	 * @return Component
	 */
	public Component visit(WhereClause n) {
		n.nodeOptional.accept(this);
		
		//TODO a scope would be added here
		bracketStack.push(EnumOperator.WHERE);
		
		Component whereClause = n.groupGraphPattern.accept(this);
		
		if(debug && whereClause != null)
			System.err.println("WhereClause = " + whereClause);
		
		return whereClause;
	}

	/**
	 * <p>
	 * Visit method will take a SolutionModifier node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents order clauses, 
	 * limit clauses, and offset clauses within a sparql query
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Imagine a SPARQL query example of the form
	 * <code>
	 * SELECT ?name
	 * WHERE { ?x foaf:name ?name }
	 * ORDER BY ?name
	 * LIMIT 20
	 * OFFSET 5
	 * </code>
	 * 
	 * <p>
	 * Since SolutionModifier is a root node in a subtree representing,
	 * order clauses, offset clauses, and/or limit clauses, then its
	 * children are the following:
	 * <ul>
	 * 	<li> NodeOptional contains OrderClause of the form <code> ORDER BY ?name </code>
	 * 	<li> NodeOptional1 contains LimitOffsetClauses of the form <code> LIMIT 20 </code>
	 * 		 or of the form <code> OFFSET 5 </code>
	 * </ul>
	 * 
	 * <PRE>
	 * nodeOptional -> ( OrderClause() )?
	 * nodeOptional1 -> ( LimitOffsetClauses() )?
	 * </PRE>
	 * 
	 * @param SolutionModifier
	 * return Component
	 */
	public Component visit(SolutionModifier n) {
		//TODO may need to figure out what to do with limit offset clauses
		Component solutionMod = n.nodeOptional.accept(this);
		n.nodeOptional1.accept(this);
		
		if(debug && solutionMod != null)
			System.err.println("SolutionModifier = " + solutionMod);
		
		return solutionMod;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a LimitOffsetClauses node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * limit clauses, and offset clauses within a sparql query
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Imagine a SPARQL query example of the form
	 * <code>
	 * SELECT ?name
	 * WHERE { ?x foaf:name ?name }
	 * ORDER BY ?name
	 * LIMIT 20
	 * OFFSET 5
	 * </code>
	 * 
	 * <p>
	 * Since LimitOffsetClauses is a root node in a subtree representing
	 * offset clauses, and/or limit clauses, then its
	 * children are the following:
	 * <ul>
	 * 	<li> NodeChoice contains LimitOffsetClauses such as
	 * 		 <code>LIMIT 20</code> and <code>OFFSET 5</code>
	 * 		 from the example above.
	 * </ul>
	 * 
	 * <PRE>
	 * nodeChoice -> ( LimitClause() ( OffsetClause() )? | OffsetClause() ( LimitClause() )? )
	 * </PRE>
	 * 
	 * @param LimitOffsetClauses
	 * @return Component
	 */
	public Component visit(LimitOffsetClauses n) {
		Component limitOffsetClauses = n.nodeChoice.accept(this);
		
		if(debug && limitOffsetClauses != null)
			System.err.println("LimitOffsetClauses = " + limitOffsetClauses);
		
		return limitOffsetClauses;
	}

	/**
	 * <p>
	 * Visit method will take a OrderClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * order clauses within a sparql query
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Imagine a SPARQL query example of the form
	 * <code>
	 * SELECT ?name
	 * WHERE { ?x foaf:name ?name }
	 * ORDER BY ?name
	 * LIMIT 20
	 * OFFSET 5
	 * </code>
	 * 
	 * <p>
	 * Since OrderClause is a root node in a subtree representing
	 * order clauses then its children are the following:
	 * <ul>
	 * 	<li> NodeToken contains <code>ORDER</code>
	 * 	<li> NodeToken1 contains <code>BY</code>
	 *  <li> NodeList contains an order condition such as 
	 *  	 <code>?name<code> in the <code>ORDER BY ?name</code>
	 *  	 clause above.
	 * </ul>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;ORDER&gt;
	 * nodeToken1 -> &lt;BY&gt;
	 * nodeList -> ( OrderCondition() )+
	 * </PRE>
	 * 
	 * @param OrderClause
	 * @return Component
	 */
	public Component visit(OrderClause n) {
		n.nodeToken.accept(this);
		n.nodeToken1.accept(this);
		Component orderClause = n.nodeList.accept(this);
		
		if(debug && orderClause != null)
			System.err.println("OrderClause = " + orderClause);
		
		return orderClause;
	}

	/**
	 * <p>
	 * Visit method will take a OrderCondition node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * order condition within an order by clause
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Imagine a SPARQL query example (taken from the main SPARQL website) 
	 * of the form
	 * <code>
	 * CONSTRUCT { [] foaf:name ?name }
	 * 	WHERE
	 * 	{ [] foaf:name ?name ;
	 * 	  site:hits ?hits .
	 * 	}
	 * 	ORDER BY desc(?hits)
	 * 	LIMIT 2
	 * </code>
	 * 
	 * <p>
	 * Since OrderCondition is a root node in a subtree representing
	 * order conditions then its children are the following:
	 * <ul>
	 * 	<li> NodeChoice contains an order condition that may contain
	 * 		 either an ASC or DESC and a BrackettedExpression or a
	 * 		 Constraint or Var. For instance, the <code>desc(?hits)</code>
	 * 		 in the order by clause in the above example, namely
	 * 		 <code>ORDER BY desc(?hits)</code>
	 * </ul>
	 * 
	 * <PRE>
	 * nodeChoice -> ( ( &lt;ASC&gt; | &lt;DESC&gt; ) BrackettedExpression() )
	 *       | ( Constraint() | Var() )
	 * </PRE>
	 * 
	 * @param OrderCondition
	 * @return Component
	 */
	public Component visit(OrderCondition n) {
		Component orderCondition = n.nodeChoice.accept(this);
		
		if(debug && orderCondition != null)
			System.err.println("OrderCondition = " + orderCondition);
		
		return orderCondition;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a LimitClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * limit clauses within a sparql query
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Imagine a SPARQL query example of the form
	 * <code>
	 * SELECT ?name
	 * WHERE { ?x foaf:name ?name }
	 * ORDER BY ?name
	 * LIMIT 20
	 * OFFSET 5
	 * </code>
	 * 
	 * <p>
	 * Since LimitClause is a root node in a subtree representing
	 * offset clauses, and/or limit clauses, then its
	 * children are the following:
	 * <ul>
	 * 	<li> NodeToken contains <code>LIMIT</code> 
	 *  <li> NodeToken1 contains an INTEGER such as <code>20</code>
	 *  	 like in the example above
	 * </ul>
	 * 
	 * <PRE>
	 * nodeToken -> &lt;LIMIT&gt;
	 * nodeToken1 -> &lt;INTEGER&gt;
	 * </PRE>
	 * 
	 * @param LimitClause
	 * @return Component
	 */
	public Component visit(LimitClause n) {
		n.nodeToken.accept(this);
		Component integer = n.nodeToken1.accept(this);
		
		if(debug && integer != null)
			System.err.println("LimitClause = " + integer);
		
		return integer;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a OffsetClause node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * offset clauses within a sparql query
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Imagine a SPARQL query example of the form
	 * <code>
	 * SELECT ?name
	 * WHERE { ?x foaf:name ?name }
	 * ORDER BY ?name
	 * LIMIT 20
	 * OFFSET 5
	 * </code>
	 * 
	 * <p>
	 * Since OffsetClause is a root node in a subtree representing
	 * offset clauses then its
	 * children are the following:
	 * <ul>
	 * 	<li> NodeToken contains <code>OFFSET</code>
	 *  <li> NodeToken1 contains an INTGER such as <code>5</code>
	 *  	 like the example shown above. 
	 * </ul>
	 *
	 * <PRE>
	 * nodeToken -> &lt;OFFSET&gt;
	 * nodeToken1 -> &lt;INTEGER&gt;
	 * </PRE>
	 * 
	 * @param OffsetClause
	 * @return Component
	 */
	public Component visit(OffsetClause n) {
		n.nodeToken.accept(this);
		Component integer = n.nodeToken1.accept(this);
		
		if(debug && integer != null)
			System.err.println("OffsetClause = " + integer);
		
		return integer;
	}

	/**
	 * <p>
	 * Visit method will take a GroupGraphPattern node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a group graph pattern (e.g. - the body of a where clause,
	 * or construct clause).
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * For example, in the following sparql query example:
	 * <code>
	 * SELECT ?p
	 * WHERE {?s ?p ?o}
	 * </code>
	 * The group graph pattern is considered <code>{?s ?p ?o}</code>
	 * 
	 * <p>
	 * The children of the parent GroupGraphPattern node are
	 * <ul>
	 * 	<li> NodeToken node contains <code>{</code>
	 *  <li> NodeOptional node contains <code>?s ?p ?o</code>
	 *  <li> NodeListOptional node contains data sets or filter clauses
	 *  <li> NodeToken1 node contains <code>}</code>
	 * </ul>
	 *  
	 * <p>
	 * <PRE>
	 * nodeToken -> "{"
	 * nodeOptional -> ( TriplesBlock() )?
	 * nodeListOptional -> ( ( GraphPatternNotTriples() | Filter() ) ( "." )? ( TriplesBlock() )? )*
	 * nodeToken1 -> "}"
	 * </PRE>
	 * 
	 * @param GroupGraphPattern
	 * @return Component
	 */
	public Component visit(GroupGraphPattern n) {
		n.nodeToken.accept(this);
		Component groupGraphPattern = n.nodeOptional.accept(this);
		n.nodeListOptional.accept(this);
		n.nodeToken1.accept(this);
		
		//TODO scope should be here if we were to implement it
		bracketStack.pop();//upon encounter of "}", scope changes so pop the stack
		
		if(debug && groupGraphPattern != null){
			System.err.println("GroupGraphPattern = " + groupGraphPattern);
		}
		
		return groupGraphPattern;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a TriplesBlock node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a a triple block.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * For instance, in the example sparql query
	 * <code>
	 * SELECT ?o
	 * WHERE{ ?s ?p ?o .
	 * 		  ?s2 ?p2 ?o2 }
	 * </code>
	 * the triple block is 
	 * <code>
	 * ?s ?p ?o .
	 * ?s2 ?p2 ?o2
	 * </code>
	 * 
	 * <p>
	 * The parent node, TriplesBlock, has children which are the following:
	 * <ul>
	 * 	<li> TriplesSameSubject contains triples
	 * 	<li> NodeOptional contains an optional triples block for additional
	 * 		 triples
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * triplesSameSubject -> TriplesSameSubject()
	 * nodeOptional -> ( "." ( TriplesBlock() )? )?
	 * </PRE>
	 * 
	 * @param TriplesBlock
	 * @return Component
	 * 
	 */
	public Component visit(TriplesBlock n) {
		Component triplesBlk = n.triplesSameSubject.accept(this);
		n.nodeOptional.accept(this);
		
		if(debug && triplesBlk != null)
			System.err.println("TriplesBlock  = " + triplesBlk);
		
		if(Triple.triples.size() > 0){
			BGP bgp = new BGP();//initialize bgp
			bgp.addTriples(Triple.triples);//add all triples to bgp
			Utilities.bgps.add(bgp);//allow bgp's to be saved somewhere besides expr tree 
			try{ exprTree.insert(bgp, bracketStack.size() - 1); }//insert bgp in tree
			catch(Exception e){ e.printStackTrace(); }
			Triple.triples.removeAll(Triple.triples);//clear triples for next block
		}
		return triplesBlk;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a GraphPatternNotTriples node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * an optional graph pattern, group or union graph pattern, or graph
	 * graph pattern
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * For instance, an example sparql query taken from the official sparql
	 * language website
	 * <code>
	 * PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
	 * SELECT ?nameX ?nameY ?nickY
	 * WHERE
	 * 	{ ?x foaf:knows ?y ;
	 * 	  foaf:name ?nameX .
	 * 	  ?y foaf:name ?nameY .
	 * 	  OPTIONAL { ?y foaf:nick ?nickY }
	 * 	}
	 * </code>
	 * In this code example the graph pattern not triples is
	 * <code> OPTIONAL { ?y foaf:nick ?nickY } </code>
	 * 
	 * <p>
	 * The parent node, GraphPatternNotTriples, has children:
	 * <ul>
	 * 	<li> OptionalGraphPattern node contains an optional graph pattern
	 * 	<li> GroupOrUnionGraphPattern node contains a union graph pattern
	 *  <li> GraphGraphPattern node contains a graph graph pattern
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> ( OptionalGraphPattern() | GroupOrUnionGraphPattern() | GraphGraphPattern() )
	 * </PRE>
	 * 
	 * @param GraphPatternNotTriples
	 * @return Component
	 */
	public Component visit(GraphPatternNotTriples n) {
		Component graphPatternNotTriples = n.nodeChoice.accept(this);
		
		if(debug && graphPatternNotTriples != null)
			System.err.println("GraphPatternNotTriples = " + graphPatternNotTriples);
		
		return graphPatternNotTriples;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a OptionalGraphPattern node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * an optional graph pattern
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * For instance, an example sparql query taken from the official sparql
	 * language website
	 * <code>
	 * PREFIX foaf:    <http://xmlns.com/foaf/0.1/>
	 * SELECT ?nameX ?nameY ?nickY
	 * WHERE
	 * 	{ ?x foaf:knows ?y ;
	 * 	  foaf:name ?nameX .
	 * 	  ?y foaf:name ?nameY .
	 * 	  OPTIONAL { ?y foaf:nick ?nickY }
	 * 	}
	 * </code>
	 * In this code example an optional graph pattern is
	 * <code> OPTIONAL { ?y foaf:nick ?nickY } </code>
	 * 
	 * <p>
	 * The parent node, OptionalGraphPattern, have the following children
	 * within the subtree:
	 * <ul>
	 * 	<li> NodeToken node contains <code>OPTIONAL</code>
	 * 	<li> GroupGraphPattern node contains a graph pattern such
	 * 		 as the pattern in the example above, namely, 
	 * 		 <code> {?y foaf:nick ?nickY} </code> 
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> &lt;OPTIONAL&gt;
	 * groupGraphPattern -> GroupGraphPattern()
	 * </PRE>
	 * 
	 * @param OptionalGraphPattern
	 * @return Component
	 */
	public Component visit(OptionalGraphPattern n) {
		n.nodeToken.accept(this);
		
		//TODO keep track of scope
		bracketStack.push(EnumOperator.OPTIONAL);
		LeftJoin leftJoin = new LeftJoin();
		try{ exprTree.insert(leftJoin, bracketStack.size() - 2); }
		catch(Exception e){ e.printStackTrace(); }
		
		Component optionalGraphPattern = n.groupGraphPattern.accept(this);
		
		if(debug && optionalGraphPattern != null)
			System.err.println("OptionalGraphPattern = " + optionalGraphPattern);
		
		return optionalGraphPattern;
	}

	/**
	 * <p>
	 * Visit method will take a GraphGraphPattern node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * an a graph graph pattern
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * For instance, the graph graph pattern would look something
	 * <code>
	 * GRAPH ?g
	 * { ?person foaf:name ?name ; foaf:mbox ?mbox }
	 * </code>
	 * 
	 * The GraphGraphPattern parent node has children:
	 * <ul>
	 * 	<li> NodeToken node contains <code>GRAPH</code>
	 * 	<li> VarOrIRIRef node contains a variable or iri reference.
	 * 		 In the example above, the variable would be <code>?g</code>
	 * 	<li> GroupGraphPattern node contains a pattern such as the
	 * 		 one in the example above <code>{ ?person foaf:name ?name ; foaf:mbox ?mbox }</code>
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> &lt;GRAPH&gt;
	 * varOrIRIref -> VarOrIRIref()
	 * groupGraphPattern -> GroupGraphPattern()
	 * </PRE>
	 * 
	 * @param GraphGraphPattern
	 * @return Component
	 */
	public Component visit(GraphGraphPattern n) {
		n.nodeToken.accept(this);
		n.varOrIRIref.accept(this);
		Component graphGraphPattern = n.groupGraphPattern.accept(this);
		
		if(debug && graphGraphPattern != null)
			System.err.println("GraphGraphPattern = " + graphGraphPattern);
		
		return graphGraphPattern;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a GroupOrUnionGraphPattern node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * an a group or union graph pattern
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * For instance, the example sparql query
	 * <code>
	 * SELECT ?title
	 * WHERE  { { ?book dc10:title  ?title } 
	 * UNION { ?book dc11:title  ?title } }
	 * </code>
	 * has a group or union graph pattern, specifically,
	 * <code>{ { ?book dc10:title  ?title } 
	 * UNION { ?book dc11:title  ?title } }
	 * </code>
	 *  
	 * <p>
	 * The GroupOrUnionGraphPattern parent has children in its subtree:
	 * <ul>
	 * 	<li> GroupGraphPattern node contains a group graph pattern.
	 * 		 The group graph pattern in the example above is
	 * 		 <code>{ ?book dc10:title  ?title } </code>
	 * 	<li> NodeListOptional node contains the union clause.
	 * 		 The example above has <code>UNION { ?book dc11:title  ?title }</code>
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * groupGraphPattern -> GroupGraphPattern()
	 * nodeListOptional -> ( &lt;UNION&gt; GroupGraphPattern() )*
	 * </PRE>
	 * 
	 * @param GroupOrUnionGraphPattern
	 * @return Component
	 */
	public Component visit(GroupOrUnionGraphPattern n) {
		Component groupOrUnionGraphPattern = n.groupGraphPattern.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && groupOrUnionGraphPattern != null)
			System.err.println("GroupOrUnionGraphPattern = " + groupOrUnionGraphPattern);
		
		return groupOrUnionGraphPattern;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a Filter node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a filter.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * In the example from the sparql specification website,
	 * <code>
	 * SELECT ?x
	 * WHERE
	 * {  ?x foaf:name ?name .
	 * ?x foaf:mbox ?mbox .
	 * FILTER regex(?name, "Smith")
	 * }
	 * </code>
	 * The filter node contains a subtree that represents
	 * <code>FILTER regex(?name, "Smith")</code>
	 * 
	 * 
	 * <p>
	 * The Filter node has children in its subtree:
	 * <ul>
	 * 	<li> NodeToken node contains <code>FILTER</code>
	 * 	<li> Constraint node contains a Constraint (e.g. - 
	 * <code>regex(?name, "Smith")</code>
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> &lt;FILTER&gt;
	 * constraint -> Constraint()
	 * </PRE>
	 * 
	 * @param Filter
	 * @return Component
	 */
	public Component visit(Filter n) {
		n.nodeToken.accept(this);
		Component filter = n.constraint.accept(this);
		
		if(debug && filter != null)
			System.err.println("Filter = " + filter);
		
		return filter;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a Constraint node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a constraint.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * Constraint is a parent node whose children:
	 * <ul>
	 * 	<li> NodeChoice contains BrackettedExpression 
	 * 		 or BuiltInCall or FunctionCall
	 * </ul>
	 * 
	 * <p>
	 * An example of a constraint could be <code>regex(?name, "Smith")</code>
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> BrackettedExpression()
	 *       | BuiltInCall()
	 *       | FunctionCall()
	 * </PRE>
	 * 
	 * @param FilterConstraint
	 * @return Component
	 */
	public Component visit(Constraint n) {
		Component constraint = n.nodeChoice.accept(this);
		
		if(debug && constraint != null)
			System.err.println("Constraint = " + constraint);
		
		return constraint;
	}

	/**
	 * <PRE>
	 * iRIref -> IRIref()
	 * argList -> ArgList()
	 * </PRE>
	 */
	public Component visit(FunctionCall n) {
		//TODO still need to implement this
		n.iRIref.accept(this);
		n.argList.accept(this);
		return null;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a ArgList node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a arg list.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> ( &lt;NIL&gt; | "(" Expression() ( "," Expression() )* ")" )
	 * </PRE>
	 * 
	 * @param ArgList
	 * @return Component
	 */
	public Component visit(ArgList n) {
		Component argList = n.nodeChoice.accept(this);
		
		if(debug && argList != null)
			System.err.println("ArgList = " + argList);
		
		return argList;
	}

	/**
	 * <p>
	 * Visit method will take a ConstructTemplate node and
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents the construct
	 * template of the contruct query
	 * 
	 * <p>
	 * The children of ConstructTemplate are
	 * <ul>
	 * 	<li> NodeToken which will contain a <code>{</code>
	 *  <li> NodeOptional which will contain construct triples
	 *  <li> NodeToken1 which will contain a <code>}</code>
	 * </ul>
	 * 
	 * <p>
	 * The visit method will return a Component which will contain an
	 * appropriate token.
	 * 
	 * <p>
	 * For Example, a construct triple looks like:
	 * <code>
	 * CONSTRUCT { ?s ?p ?o }
	 * </code>
	 * where <code> {?s ?p ?o} </code> is the triple in ConstructTriples
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> "{"
	 * nodeOptional -> ( ConstructTriples() )?
	 * nodeToken1 -> "}"
	 * </PRE>
	 * 
	 * @param ConstructTemplate
	 * @return Component
	 */
	public Component visit(ConstructTemplate n) {
		n.nodeToken.accept(this);
		Component constructTemplate = n.nodeOptional.accept(this);
		n.nodeToken1.accept(this);
		
		//TODO this is where the scope used to be
		if(debug && constructTemplate != null)
			System.err.println("ConstructTempalte = " + constructTemplate);
		
		return constructTemplate;
	}

	/**
	 * <p>
	 * Visit method will take a ConstructTriples node and
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents construct triples
	 * within a construct query.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The children of the ConstructTriples subtree, where ConstructTriples
	 * is the root, are the following:
	 * <ul>
	 * 	<li> TriplesSameSubject is a child node that contains a triple
	 * 	<li> NodeOptional is a child node that may contain more triples
	 * 		 by usage of the dot operator
	 * </ul>
	 * 
	 * <p>
	 * For example, here is an example of construct triples:
	 * <code>
	 * CONSTRUCT{
	 * 		?s ?p ?o .
	 *  	?s2 ?p2 ?o2
	 * }
	 * </code>
	 * The first triple <code> ?s ?p ?o </code> is processed by TriplesSameSubject
	 * while the second triple <code> . ?s2 ?p2 ?o2 </code> is processed by NodeOptional
	 * 
	 * <p>
	 * <PRE>
	 * triplesSameSubject -> TriplesSameSubject()
	 * nodeOptional -> ( "." ( ConstructTriples() )? )?
	 * </PRE>
	 * 
	 * @param ConstructTriples
	 * @return Component
	 */
	public Component visit(ConstructTriples n) {
		Component constructTriples = n.triplesSameSubject.accept(this);
		n.nodeOptional.accept(this);
		
		if(debug && constructTriples != null)
			System.err.println("ConstructTriples " + constructTriples);
		
		return constructTriples;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a TriplesSameSubject node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a triples same subject.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> VarOrTerm() PropertyListNotEmpty()
	 *       | TriplesNode() PropertyList()
	 * </PRE>
	 * 
	 * @param TriplesSameSubject
	 * @return Component
	 */
	public Component visit(TriplesSameSubject n) {
		tripleId++;
		Triple.triples.add(new Triple());//add a new triple
		Component triplesSameSubject = n.nodeChoice.accept(this);
		
		if(debug && triplesSameSubject != null)
			System.err.println("TriplesSameSubject = " + triplesSameSubject);
		if(debug){
			int lastIdx = Triple.triples.size() - 1;
			System.err.println("Triple = " + Triple.triples.get(lastIdx));
		}
		
		
		return triplesSameSubject;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a PropertyListNotEmpty node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a property list that isn't empty.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * verb -> Verb()
	 * objectList -> ObjectList()
	 * nodeListOptional -> ( ";" ( Verb() ObjectList() )? )*
	 * </PRE>
	 * 
	 * @param PropertyListNotEmpty
	 * @return Component
	 */
	public Component visit(PropertyListNotEmpty n) {
		Component propListNotEmpty = n.verb.accept(this);
		n.objectList.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && propListNotEmpty != null)
			System.err.println("PropertyListNotEmpty = " + propListNotEmpty);
		
		return propListNotEmpty;
	}

	/** 
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * <p>
	 * <PRE>
	 * nodeOptional -> ( PropertyListNotEmpty() )?
	 * </PRE>
	 * 
	 * @param PropertyList
	 * @return Component
	 */
	public Component visit(PropertyList n) {
		Component propList = n.nodeOptional.accept(this);
		
		if(debug && propList != null)
			System.err.println("PropertyList = " + propList);
		
		return propList;
	}

	/**
	 * <p>
	 * Visit method will take a ObjectList node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a object list.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * object -> Object()
	 * nodeListOptional -> ( "," Object() )*
	 * </PRE>
	 * 
	 * @param ObjectList
	 * @return Component
	 */
	public Component visit(ObjectList n) {
		Component objectList = n.object.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && objectList != null)
			System.err.println("ObjectList = " + objectList.getRawImg());
		
		return objectList;
	}

	/**
	 * <p>
	 * Visit method will take a Object node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * an object.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <PRE>
	 * graphNode -> GraphNode()
	 * </PRE>
	 * 
	 * @param Object
	 * @return Component
	 */
	public Component visit(Object n) {
		Component obj = n.graphNode.accept(this);
		
		//capture token
		java.lang.String tokenImg = obj.getRawImg();
		
		//set object for triple
		if(Utilities.isKey(tokenImg)){
			obj.setObjectKey();
			updateTriple(obj, "o"); //update triple with object
			//update first component encountered
			bookKeeper.updateFirstEncounteredComponent(obj, "object key");
		}
		else{
			obj.setObject();
			updateTriple(obj, "o"); //update triple with object
			//update first component encountered
			bookKeeper.updateFirstEncounteredComponent(obj, "object");
		}
		
		if(debug && obj != null)
			System.err.println("Object = " + obj);
		
		return obj;
	}

	/**
	 * <p>
	 * Visit method will take a Verb node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a verb.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> VarOrIRIref()
	 *       | &lt;TYPE_A&gt;
	 *       | PutativeLiteral()
	 * </PRE>
	 * 
	 * @param Verb
	 * @return Component
	 */
	public Component visit(Verb n) {
		Component verb = null;//component to capture verb
		java.lang.String verbToken =n.nodeChoice.choice.toString();//capture token
		if(!verbToken.equals("a")){ verb = n.nodeChoice.accept(this); }
		else{
			verb = new Component(verbToken, tripleId);
			bookKeeper.addComponent(verb);
		}
		
		//if spo is false, then set predicate
		if(!verb.isSubject() && !verb.isSubjectKey() &&
		   !verb.isPredicate() && !verb.isObject() && !verb.isObjectKey()){
			verb.setPredicate();
			updateTriple(verb, "p"); //update triple with predicate
			//update first component encountered
			bookKeeper.updateFirstEncounteredComponent(verb, "predicate");
		}
		
		if(debug && verb != null){
			System.err.println("Verb = " + verb);
		}
		
		return verb;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a TriplesNode node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a triples node
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The TriplesNode is a parent of the child NodeChoice which
	 * contains either a Collection or a BlankNodePropertyList
	 * 
	 * <PRE>
	 * nodeChoice -> Collection()
	 *       | BlankNodePropertyList()
	 * </PRE>
	 * 
	 * @param TriplesNode
	 * @return Component
	 */
	public Component visit(TriplesNode n) {
		Component triplesNode = n.nodeChoice.accept(this);
		
		if(debug && triplesNode != null)
			System.err.println("TriplesNode = " + triplesNode);
		
		return triplesNode;
	}

	/**
	 * <p>
	 * Visit method will take a BlankNodePropertyList node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a blank node property list
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> "["
	 * propertyListNotEmpty -> PropertyListNotEmpty()
	 * nodeToken1 -> "]"
	 * </PRE>
	 * 
	 * @param BlankNodePropertyList
	 * @return Component
	 */
	public Component visit(BlankNodePropertyList n) {
		n.nodeToken.accept(this);
		Component blankNodePropList = n.propertyListNotEmpty.accept(this);
		n.nodeToken1.accept(this);
		
		if(debug && blankNodePropList != null)
			System.err.println("BlankNodePropertyList = " + blankNodePropList);
		
		return blankNodePropList;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a Collection node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a collection
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The Collection node is a parent of the following children:
	 * <ul>
	 * 	<li> NodeToken node contains <code>{</code>
	 * 	<li> NodeList node contains a graph node
	 * 	<li> NodeToken1 node contains <code>}</code>
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> "("
	 * nodeList -> ( GraphNode() )+
	 * nodeToken1 -> ")"
	 * </PRE>
	 * 
	 * @param Collection
	 * @return Component
	 */
	public Component visit(Collection n) {
		/*
		n.nodeToken.accept(this);
		n.nodeList.accept(this);
		n.nodeToken1.accept(this);
		return null;
		*/
		//TODO I don't know if I'm doing the right thing here or not
		n.nodeToken.accept(this);
		for(Node e : n.nodeList.nodes){
			Component collect = e.accept(this);
			
			if(debug && collect != null)
				System.err.println("Collection = " + collect);
			
		}
		n.nodeToken1.accept(this);
		return null;
		
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a GraphNode node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a graph node.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> VarOrTerm()
	 *       | TriplesNode()
	 * </PRE>
	 * 
	 * @param GraphNode
	 * @return Component
	 */
	public Component visit(GraphNode n) {
		Component graphNode = n.nodeChoice.accept(this);
		
		if(debug && graphNode != null){
			System.err.println("GraphNode = " + graphNode);
		}
		
		return graphNode;
	}

	/**
	 * <p>
	 * Visit method will take a VarOrTerm node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a variable or a term
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The child of VarOrTerm is a NodeChoice which can 
	 * be either a Var or GraphTerm. 
	 * For instance <code>?x</code> or <code>bsbm-inst:product1</code>
	 * 
	 * <p>
	 * Since a variable or term could be a component of an expression, triple,
	 * or other clause, we wrap this variable or term in a Component class.
	 * This component is what is returned.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> Var()
	 *       | GraphTerm()
	 * </PRE>
	 * 
	 * @param VarOrTerm
	 * @return Component
	 */
	public Component visit(VarOrTerm n) {
		Component varOrTerm = n.nodeChoice.accept(this);
		
		//capture a raw token image
		java.lang.String varOrTermToken = varOrTerm.getRawImg();
		
		//if not predicate, object, or object key
		if(!varOrTerm.isPredicate() && !varOrTerm.isObject() && !varOrTerm.isObjectKey()){
			if(Utilities.isKey(varOrTermToken)){//then if key
				varOrTerm.setSubjectKey(); //set subject key flag
				updateTriple(varOrTerm, "s"); //update triple with subject
				//update first encountered
				bookKeeper.updateFirstEncounteredComponent(varOrTerm, "subject key");
			}
			else{//else set subject
				varOrTerm.setSubject();
				updateTriple(varOrTerm, "s"); //update triple with subject
				//update first encountered
				bookKeeper.updateFirstEncounteredComponent(varOrTerm, "subject");
			}
		}
		
		if(debug){
			if(varOrTerm != null){
				System.err.println("VarOrTerm = " + varOrTerm);
			}
		}
		
		return varOrTerm;
	}

	/**
	 * <p>
	 * Visit method will take a VarOrIRIRef node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a variable or iri reference.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> Var()
	 *       | IRIref()
	 * </PRE>
	 * 
	 * @param VarOrIRIref
	 * @return Component
	 */
	public Component visit(VarOrIRIref n) {
		Component varOrIRIref = n.nodeChoice.accept(this);
		if(debug){
			if(varOrIRIref != null) 
				System.err.println("VarOrIRIRef = " + varOrIRIref);
		}
		return varOrIRIref;
	}

	/**
	 * <p>
	 * Visit method will take a Var node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents
	 * a variable within a sparql query.
	 * 
	 * <p>
	 * This method will return a component that contains an
	 * appropriate token for each invocation of the method.
	 * 
	 * <p>
	 * Since Var is a root node in a subtree representing
	 * a variable, then its children are the following:
	 * <ul>
	 * 	<li> NodeChoice contains a variable such as <code>?s</code>
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;VAR1&gt;
	 *       | &lt;VAR2&gt;
	 * </PRE>
	 * 
	 * @param Var
	 * @return Component
	 */
	public Component visit(Var n) {
		n.nodeChoice.accept(this);//process var
		java.lang.String varToken = n.nodeChoice.choice.toString();//capture token
		Component var = new Component(varToken, tripleId);//create var component
		if(tripleId == 0) selectVars.add(var);
		bookKeeper.addComponent(var);//add to first encountered components set
		if(debug){ System.err.println("Var = " + var); }//debug statement
		return var;//return var component
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a GraphTerm node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a graph term.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> IRIref()
	 *       | RDFLiteral()
	 *       | NumericLiteral()
	 *       | BooleanLiteral()
	 *       | BlankNode()
	 *       | PutativeLiteral()
	 *       | &lt;NIL&gt;
	 * </PRE>
	 * 
	 * @param GraphTerm
	 * @return Component
	 */
	public Component visit(GraphTerm n) {
		Component graphTerm = n.nodeChoice.accept(this);
		
		if(debug){
			if(graphTerm != null){
				System.err.println("GraphTerm = " + graphTerm);
			}
		}
		
		return graphTerm;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a Expression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The Expression parent has a child called ConditionalOrExpression.
	 * 
	 * <p>
	 * <PRE>
	 * conditionalOrExpression -> ConditionalOrExpression()
	 * </PRE>
	 * 
	 * @param Expression
	 * @return Component
	 */
	public Component visit(Expression n) {
		Component expr = n.conditionalOrExpression.accept(this);
		
		if(debug && expr != null)
			System.err.println("Expression  = " + expr);
		
		return expr;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a ConditionalOrExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a conditional or expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * conditionalAndExpression -> ConditionalAndExpression()
	 * nodeListOptional -> ( "||" ConditionalAndExpression() )*
	 * </PRE>
	 * 
	 * @param ConditionalOrExpression
	 * @return Component
	 */
	public Component visit(ConditionalOrExpression n) {
		Component condOrExpr = n.conditionalAndExpression.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && condOrExpr != null)
			System.err.println("ConditionalOrExpression = " + condOrExpr);
		
		return condOrExpr;
	}

	/**
	 * <p>
	 * Visit method will take a ConditionalAndExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a conditional and expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * This ConditionalAndExpression parent has children:
	 * <ul>
	 * 	<li>ValueLogical node contains a logical value
	 * 	<li>NodeListOptional node contains a conjunction followed by another
	 * 		logical value
	 * </ul> 
	 * 
	 * <p>
	 * <PRE>
	 * valueLogical -> ValueLogical()
	 * nodeListOptional -> ( "&&" ValueLogical() )*
	 * </PRE>
	 * 
	 * @param ConditionalAndExpression
	 * @return Component
	 */
	public Component visit(ConditionalAndExpression n) {
		Component condAndExpr = n.valueLogical.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && condAndExpr != null)
			System.err.println("ConditionalAndExpression = " + condAndExpr);
		
		return condAndExpr;
	}

	/**
	 * <p>
	 * Visit method will take a ValueLogical node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a logical value.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * A ValueLogial node has children in its subtree, namely:
	 * <ul>
	 * 	<li> RelationalExpression
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * relationalExpression -> RelationalExpression()
	 * </PRE>
	 * 
	 * @param ValueLogical
	 * @return Component
	 */
	public Component visit(ValueLogical n) {
		Component valueLogical = n.relationalExpression.accept(this);
		
		if(debug && valueLogical != null)
			System.err.println("ValueLogical = " + valueLogical);
		
		return valueLogical;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * numericExpression -> NumericExpression()
	 * nodeOptional -> ( "=" NumericExpression() | "!=" NumericExpression() | "&lt;" NumericExpression() | "&gt;" NumericExpression() | "&lt;=" NumericExpression() | "&gt;=" NumericExpression() )?
	 * </PRE>
	 * 
	 * @param RelationalExpression
	 * @return Component
	 */
	public Component visit(RelationalExpression n)
	{
		
		filteredConstraint = new RelationalExpr();
		
		Component relationalExpr = n.numericExpression.accept(this);
		
		n.nodeOptional.accept(this);
		
		if(debug && relationalExpr != null)
			System.err.println("RelationalExpression = " + relationalExpr);
		
		if(debug)
		{
			System.err.println("Operand1 = " + filteredConstraint.getOperand1());
			System.err.println("Operator = " + filteredConstraint.getOperator());
			System.err.println("Operand2 = " + filteredConstraint.getOperand2());
		}
		
		try
		{
			//add constraint to appropriate triple
			Utilities.findTripleForConstraint(Triple.triples, filteredConstraint);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return relationalExpr;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a NumericExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a numeric expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * additiveExpression -> AdditiveExpression()
	 * </PRE>
	 * 
	 * @param NumericExpression
	 * @return Component
	 */
	public Component visit(NumericExpression n) {
		Component numericExpr = n.additiveExpression.accept(this);
		
		//Since numeric expressions are only used in relational expression
		//then filtered constraint is activated
		if(filteredConstraint != null)
		{
			//if operand1 is null, then set it to the numeric expression
			if(filteredConstraint.getOperand1() == null)
				filteredConstraint.setOperand1(numericExpr);
			
			//if operand2 is null, then set it to the numeric expression
			else if(filteredConstraint.getOperand2() == null)
				filteredConstraint.setOperand2(numericExpr);
		}
		
		if(debug)
			System.err.println("NumericExpression = " + numericExpr);
		
		return numericExpr;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * multiplicativeExpression -> MultiplicativeExpression()
	 * nodeListOptional -> ( "+" MultiplicativeExpression() | "-" MultiplicativeExpression() | NumericLiteralPositive() | NumericLiteralNegative() )*
	 * </PRE>
	 * 
	 * @param AdditiveExpression
	 * @return Component
	 */
	public Component visit(AdditiveExpression n) {
		Component additiveExpr = n.multiplicativeExpression.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && additiveExpr != null)
			System.err.println("AdditiveExpression = " + additiveExpr);
		
		return additiveExpr;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a MultiplicativeExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a multiplicative expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * unaryExpression -> UnaryExpression()
	 * nodeListOptional -> ( "*" UnaryExpression() | "/" UnaryExpression() )*
	 * </PRE>
	 * 
	 * @param MultiplicativeExpression
	 * @return Component
	 */
	public Component visit(MultiplicativeExpression n) {
		Component multExpr = n.unaryExpression.accept(this);
		n.nodeListOptional.accept(this);
		
		if(debug && multExpr != null)
			System.err.println("MultiplicativeExpression = " + multExpr);
		
		return multExpr;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a UnaryExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a unary expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> ( "!" PrimaryExpression() | "+" PrimaryExpression() | "-" PrimaryExpression() | PrimaryExpression() )
	 * </PRE>
	 * 
	 * @param UnaryExpression
	 * @return Component
	 */
	public Component visit(UnaryExpression n) {
		Component unaryExpr = n.nodeChoice.accept(this);
		
		if(debug && unaryExpr != null)
			System.err.println("UnaryExpression = " + unaryExpr);
		
		return unaryExpr;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a PrimaryExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a primary expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> BrackettedExpression()
	 *       | BuiltInCall()
	 *       | IRIrefOrFunction()
	 *       | RDFLiteral()
	 *       | NumericLiteral()
	 *       | BooleanLiteral()
	 *       | Var()
	 * </PRE>
	 * 
	 * @param PrimaryExpression
	 * @return Component
	 */
	public Component visit(PrimaryExpression n) {
		Component primaryExpr = n.nodeChoice.accept(this);
		
		if(debug && primaryExpr != null) 
			System.err.println("PrimaryExpression = " + primaryExpr);
		
		return primaryExpr;
	}

	/**
	 * <p>
	 * Visit method will take a BrackettedExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a bracketted expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * The parent BrackettedExpression node has children:
	 * <ul>
	 * 	<li> NodeToken node contains <code>(</code>
	 *  <li> Expression node contains <code>( bound(?date) )</code>
	 *  <li> NodeToken1 node contains <code>)</code>
	 * </ul>
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> "("
	 * expression -> Expression()
	 * nodeToken1 -> ")"
	 * </PRE>
	 * 
	 * @param BrackettedExpression
	 * @return Component
	 */
	public Component visit(BrackettedExpression n) {
		n.nodeToken.accept(this);
		Component brackettedExpr = n.expression.accept(this);
		n.nodeToken1.accept(this);
		
		if(debug && brackettedExpr != null)
			System.err.println("BrackettedExpression = " + brackettedExpr);
		
		return brackettedExpr;
	}

	/**
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> ( &lt;STR&gt; "(" Expression() ")" | &lt;LANG&gt; "(" Expression() ")" | &lt;LANGMATCHES&gt; "(" Expression() "," Expression() ")" | &lt;DATATYPE&gt; "(" Expression() ")" | &lt;BOUND&gt; "(" Var() ")" | &lt;SAME_TERM&gt; "(" Expression() "," Expression() ")" | &lt;IS_IRI&gt; "(" Expression() ")" | &lt;IS_URI&gt; "(" Expression() ")" | &lt;IS_BLANK&gt; "(" Expression() ")" | &lt;IS_LITERAL&gt; "(" Expression() ")" | RegexExpression() )
	 * </PRE>
	 * 
	 * @param BuiltInCall
	 * @return Component
	 */
	public Component visit(BuiltInCall n) {
		Component builtInCall = n.nodeChoice.accept(this);
		
		if(debug && builtInCall != null)
			System.err.println("BuiltInCall = " + builtInCall);
		
		return builtInCall;
	}

	/**
	 * <p>
	 * Visit method will take a RegexExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a regex expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> &lt;REGEX&gt;
	 * nodeToken1 -> "("
	 * expression -> Expression()
	 * nodeToken2 -> ","
	 * expression1 -> Expression()
	 * nodeOptional -> ( "," Expression() )?
	 * nodeToken3 -> ")"
	 * </PRE>
	 * 
	 * @param RegexExpression
	 * @return Component
	 */
	public Component visit(RegexExpression n) {
		//TODO still need to implement something here
		n.nodeToken.accept(this);
		n.nodeToken1.accept(this);
		n.expression.accept(this);
		n.nodeToken2.accept(this);
		n.expression1.accept(this);
		n.nodeOptional.accept(this);
		n.nodeToken3.accept(this);
		return null;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * iRIref -> IRIref()
	 * nodeOptional -> ( ArgList() )?
	 * </PRE>
	 * 
	 * @param IRIrefOrFunction
	 * @return Component
	 */
	public Component visit(IRIrefOrFunction n) {
		Component iriRefOrFn = n.iRIref.accept(this);
		n.nodeOptional.accept(this);
		
		if(debug && iriRefOrFn != null)
			System.err.println("IRIrefOrFunction = " + iriRefOrFn);
		
		return iriRefOrFn;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a RdfLiteral node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a rdf literal.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * string -> String()
	 * nodeOptional -> ( &lt;LANGTAG&gt; | ( "^^" IRIref() ) )?
	 * </PRE>
	 * 
	 * @param RDFLiteral
	 * @return Component
	 */
	public Component visit(RDFLiteral n) {
		Component rdfLiteral = n.string.accept(this);
		n.nodeOptional.accept(this);
		
		if(debug && rdfLiteral != null)
			System.err.println("RdfLiteral = " + rdfLiteral);
		
		return rdfLiteral;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a NumericLiteral node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a numeric literal.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> ( NumericLiteralUnsigned() | NumericLiteralPositive() | NumericLiteralNegative() )
	 * </PRE>
	 * 
	 * @param NumericLiteral
	 * @return Component
	 */
	public Component visit(NumericLiteral n) {
		Component numericLiteral = n.nodeChoice.accept(this);
		
		if(debug && numericLiteral != null)
			System.err.println("NumericLiteral = " + numericLiteral);
		
		return numericLiteral;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a NumericLiteralUnsigned node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a unsigned numeric literal. 
	 * For unsigned numbers, whether it be integer, decimal or double
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate token
	 * for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;INTEGER&gt;
	 *       | &lt;DECIMAL&gt;
	 *       | &lt;DOUBLE&gt;
	 * </PRE>
	 * 
	 * @param NumericLiteralUnsigned
	 * @return Component
	 */
	public Component visit(NumericLiteralUnsigned n) {
		Component numUnsigned = n.nodeChoice.accept(this);
		
		if(debug && numUnsigned != null)
			System.err.println("NumericLiteralUnsigned = " + numUnsigned);
		
		return numUnsigned;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a NumericLiteralPositive node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a positive numeric literal. 
	 * For Positive numbers, whether it be integer, decimal or double
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate token
	 * for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;INTEGER_POSITIVE&gt;
	 *       | &lt;DECIMAL_POSITIVE&gt;
	 *       | &lt;DOUBLE_POSITIVE&gt;
	 * </PRE>
	 * 
	 * @param NumericLiteralPositive
	 * @return Component
	 */
	public Component visit(NumericLiteralPositive n) {
		Component numPos = n.nodeChoice.accept(this);
		
		if(debug && numPos != null)
			System.err.println("NumericLiteralPositive = " + numPos);
		
		return numPos;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a NumericLiteralNegative node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a negative numeric literal. 
	 * For Negative numbers, whether it be integer, decimal or double
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate token
	 * for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;INTEGER_NEGATIVE&gt;
	 *       | &lt;DECIMAL_NEGATIVE&gt;
	 *       | &lt;DOUBLE_NEGATIVE&gt;
	 * </PRE>
	 * 
	 * @param NumericLiteralNegative
	 * @return Component
	 */
	public Component visit(NumericLiteralNegative n) {
		Component numNeg = n.nodeChoice.accept(this);
		
		if(debug && numNeg != null)
			System.err.println("NumericLiteralNegative = " + numNeg);
		
		return numNeg;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a BooleanLiteral node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a boolean literal.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;TRUE&gt;
	 *       | &lt;FALSE&gt;
	 * </PRE>
	 * 
	 * @param BooleanLiteral
	 * @return Component
	 */
	public Component visit(BooleanLiteral n) {
		n.nodeChoice.accept(this);
		return null;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a String node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a string.
	 * 
	 * An Example would be <code>regex(?name, "doe")</code>
	 * where <code>"doe"</code>
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;STRING_LITERAL1&gt;
	 *       | &lt;STRING_LITERAL2&gt;
	 *       | &lt;STRING_LITERAL_LONG1&gt;
	 *       | &lt;STRING_LITERAL_LONG2&gt;
	 * </PRE>
	 * 
	 * @param String
	 * @return Component
	 */
	public Component visit(String n) {
		Component str = n.nodeChoice.accept(this);
		if(debug){
			if(str != null)
				System.err.println("String = " + str);
		}
		return str;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;IRI_REF&gt;
	 *       | PrefixedName()
	 * </PRE>
	 * 
	 * @param IRIref
	 * @return Component
	 */
	public Component visit(IRIref n) {
		Component iriRef = n.nodeChoice.accept(this);
		
		if(debug){
			if(iriRef != null){
				System.err.println("IRIref = " + iriRef);
			}
		}
		
		return iriRef;
	}

	/**
	 * <PRE>
	 * nodeChoice -> &lt;PNAME_LN&gt;
	 *       | &lt;PNAME_NS&gt;
	 * </PRE>
	 * 
	 * @param PrefixedName
	 * @return Component
	 */
	public Component visit(PrefixedName n) {
		n.nodeChoice.accept(this);
		
		java.lang.String prefixedNameToken = n.nodeChoice.choice.toString();
		Component prefixedName = new Component(prefixedNameToken, tripleId);
		bookKeeper.addComponent(prefixedName);//add to first encountered components list
		
		if(debug){
			if(prefixedNameToken != null && prefixedName != null){
				System.err.println("PrefixedName = " + prefixedName);
			}
		}
		
		return prefixedName;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a RelationalExpression node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a relational expression.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeChoice -> &lt;BLANK_NODE_LABEL&gt;
	 *       | &lt;ANON&gt;
	 * </PRE>
	 * 
	 * @param BlankNode
	 * @return Component
	 */
	public Component visit(BlankNode n) {
		Component blankNode = n.nodeChoice.accept(this);
		
		if(debug){
			if(blankNode != null)
				System.err.println("BlankNode = " + blankNode);
		}
		
		return blankNode;
	}

	/**
	 * 
	 * <p>
	 * Visit method will take a PutativeLiteral node and 
	 * traverse through children of subtree in depth first manner.
	 * This node is responsible for a tree that represents 
	 * a putative literal.
	 * 
	 * <p>
	 * This method will return a component that contains an appropriate
	 * token for each invocation of the method.
	 * 
	 * <p>
	 * <PRE>
	 * nodeToken -> &lt;PUTATIVE_LITERAL&gt;
	 * </PRE>
	 * 
	 * @param PutativeLiteral
	 * @return Component
	 */
	public Component visit(PutativeLiteral n) {
		Component putativeLiteral = n.nodeToken.accept(this);
		
		if(debug){
			if(putativeLiteral != null)
				System.err.println("PutativeLiteral = " + putativeLiteral);
		}
		
		return putativeLiteral;
	}

}